package com.nf.health.app.intervention_details.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.intervention_details.adapter.HistorySchemeAdapter;
import com.nf.health.app.models.SchemeHistoryBean;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 历史方案接口
 * Created by Administrator on 2016/11/16.
 */
public class HistorySchemeActivity extends BaseActivity {

    HistorySchemeAdapter historySchemeAdapter;

    List<SchemeHistoryBean> schemeHistoryBeans;
    private List<SchemeHistoryBean> contentList = new ArrayList<SchemeHistoryBean>();
    int pageno = 1,pagesize = 20;

    @Bind(R.id.history_scheme)
    DragListView historyScheme;//listview 列表

    List<SchemeHistoryBean> list = new ArrayList<SchemeHistoryBean>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_history_scheme);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "历史方案");
        initView();

    }

    private void initView() {

        historyScheme.setRefreshableAndLoadMoreable(true, true);
        historyScheme.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                pageno = 1;
                initData("refresh");
            }

            @Override
            public void onLoadMore() {
                pageno++;
                initData("more");
            }
        });
    }

    private void initData(String flag) {
        provider.getHistoryScheme(pageno + "", pagesize + "", flag);
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if("refresh".equals(actionName)){
            historyScheme.completeRefresh();
            schemeHistoryBeans = (List<SchemeHistoryBean>) object;
            contentList.clear();
            contentList.addAll(schemeHistoryBeans);
            historySchemeAdapter = new HistorySchemeAdapter(this,contentList);
            historyScheme.setAdapter(historySchemeAdapter);
        }else if("more".equals(actionName)){
            historyScheme.completeLoadMore();
            schemeHistoryBeans = (List<SchemeHistoryBean>) object;
            int more_size = schemeHistoryBeans.size();
            if(more_size == 0){
                showToast("没有更多数据");
            }else{
                contentList.addAll(schemeHistoryBeans);
                historySchemeAdapter.notifyDataSetChanged();
            }
        }


        historyScheme.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
//                if((Float.parseFloat(schemeHistoryBeans.get(position-1).getEndtime())> 0)){
//                    bundle.putString("id",schemeHistoryBeans.get(position-1).getPackid());
//                    ActivityUtils.showActivity(HistorySchemeActivity.this,InterventionDetailsActivity.class,bundle);
//                }else{
                    bundle.putString(InterventionDetailsActivity.ID_FROM_HISTORY,contentList.get(position-1).getPackid());
                    ActivityUtils.showActivity(HistorySchemeActivity.this, InterventionDetailsActivity.class,bundle);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        pageno = 1 ;
         provider.getHistoryScheme(pageno + "", pagesize + "", "refresh");
    }
}
